'''
Created on Aug 6, 2013

@author: Yuanpeng_Li
Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes,
write a method to rotate the image by 90 degrees. Can you do this in place?
'''

def rotate(matrix):
    n = len(matrix)
    height = int(n / 2)
    width = height;
    if n % 2 != 0:
        width += 1
    for i in range(0, height):
        for j in range(0, width):
            temp = matrix[i][j]
            matrix[i][j] = matrix[j][n-1-i]
            matrix[j][n-1-i] = matrix[n-1-i][n-1-j]
            matrix[n-1-i][n-1-j] = matrix[n-1-j][i]
            matrix[n-1-j][i] = temp
    return matrix

def print_matrix(m):
    for i in m:
        print (i)

if __name__ == '__main__':
#    matrix = [[1, 2], [3, 4]]
    matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    print_matrix(matrix)
    matrix = rotate(matrix)
    print_matrix(matrix)